import 'dart:convert';

import 'package:bruno/bruno.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_shipper/AppConfig/Config.dart';
import 'package:sph_shipper/AppConfig/LocalStorage.dart';
import 'package:sph_shipper/AppConfig/ShowDialog.dart';

import '../TabBar/TabbarView.dart';
import '../http/api.dart';

class EvaluationPage extends StatefulWidget {
  const EvaluationPage({super.key, required this.dataMap});
  final Map dataMap;
  @override
  State<EvaluationPage> createState() => _EvaluationPageState();
}

class _EvaluationPageState extends State<EvaluationPage> {
int chooseType=3;
List goodMesList=['认真负责',"车厢整洁","主动解决问题","主动配合装卸","驾驶技术好","车况好","安全送达","准时到达装卸货地"];
List midMesList=["希望更加认真负责","希望车辆更加干净","希望服务态度更好","希望卸货更加准时"];
List badMesList=["态度差","迟到","车辆不符","不配合装卸货","多收运费","多次联系不上","非本人承运","货物丢失损坏","私自配货","倒货"];
List chooseList=[];
String message="";
@override
  void initState() {
    // TODO: implement initState
    super.initState();

  }
  void addEvaluation(){
    ApiManager.postData(
      context,
      'api/auth/shipper/evaluation/add',
      {
        "waybillId":widget.dataMap['id'],
        "driverId": widget.dataMap['driverId'],
        "shipperId": widget.dataMap['shipperId'],
        "route": widget.dataMap['goodsInfo']['loadPlaceInfo'][0]['city']+widget.dataMap['goodsInfo']['loadPlaceInfo'][0]['county']+","+
                  widget.dataMap['goodsInfo']['unloadPlaceInfo'][1]['city']+widget.dataMap['goodsInfo']['unloadPlaceInfo'][1]['county'],
        "level": chooseType==3?"POSITIVE":chooseType==2?"NEUTRAL":"NEGATIVE",
        "tags": chooseList,
        "description":message
      },
          (responseData) {
        setState(() {
          BrnToast.show("评价成功", context);
          Navigator.pushReplacement(
            context,
            MaterialPageRoute(builder: (context) => TabbarView(pateIndex: 2,),fullscreenDialog: true,),
          );
        });
      },
    );
  }
@override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
        appBar: AppBar(
        title:Text( "评价司机",style: TextStyle(fontWeight: FontWeight.w300,fontSize: 16.sp),),
      backgroundColor: Colors.white,
       centerTitle: true,
        ),
      body:Column(
        children: [
          Expanded(
              child:   ListView(
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 18.h,left:22.w),
                    child: Row(
                      children: [
                        Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.all(Radius.circular(10))),
                          child:  widget.dataMap['driverInfo']['avatarPath']!=""&&widget.dataMap['driverInfo']['avatarPath']!=null?  ClipRRect(
                            borderRadius: BorderRadius.circular(10.0), // 设置圆角半径
                            child: Image.network(
                              MyServiceUrl.imgHeader + widget.dataMap['driverInfo']['avatarPath'],
                              width: 40.w,
                              height: 40.h,
                              fit: BoxFit.cover, // 图片填充方式，可以根据需要调整
                            ),
                          ):Image.asset("lib/Images/login/默认头像.jpeg",width: 36.w,height: 36.h, fit: BoxFit.cover,),
                          ),


                        SizedBox(width: 13.w,),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Text(widget.dataMap['driverInfo']['realName'].toString()+"丨"+widget.dataMap['goodsInfo']['cargoInfo'][0]['cargoName'].toString()+"丨",style: TextStyle(fontSize: 14.sp,color: Colors.black),),
                                if(widget.dataMap['goodsInfo']['cargoInfo'][0]['cargoWeight']!=null)
                                  Text(widget.dataMap['goodsInfo']['cargoInfo'][0]['cargoWeight'].toString()+"吨"+"丨",style: TextStyle(fontSize: 14.sp,color: Colors.black),),
                                if(widget.dataMap['goodsInfo']['cargoInfo'][0]['cargoVolume']!=null)
                                  Text(widget.dataMap['goodsInfo']['cargoInfo'][0]['cargoVolume'].toString()+"方",style: TextStyle(fontSize: 14.sp,color: Colors.black),),
                              ],
                            ),
                            Row(
                              children: [
                                Text(widget.dataMap['goodsInfo']['loadPlaceInfo'][0]['city']+" "+widget.dataMap['goodsInfo']['loadPlaceInfo'][0]['county'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                                if(widget.dataMap['goodsInfo']['loadPlaceInfo'].length==2)
                                Text(","+widget.dataMap['goodsInfo']['loadPlaceInfo'][1]['city']+" "+widget.dataMap['goodsInfo']['loadPlaceInfo'][1]['county'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                              ],
                            ),
                            Row(
                              children: [
                                Text(widget.dataMap['goodsInfo']['unloadPlaceInfo'][0]['city']+" "+widget.dataMap['goodsInfo']['unloadPlaceInfo'][0]['county'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                                if(widget.dataMap['goodsInfo']['unloadPlaceInfo'].length==2)
                                  Text(","+widget.dataMap['goodsInfo']['unloadPlaceInfo'][1]['city']+" "+widget.dataMap['goodsInfo']['unloadPlaceInfo'][1]['county'],style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                              ],
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 10.w,right: 10.w,top:10.h ),
                    padding: EdgeInsets.only(left: 12.w,top: 18.h,right: 21.w,bottom: 24.h),
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [Color.fromRGBO(254,246,240,1), Colors.white],
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(10))),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Image.asset("lib/Images/OrderList/评价圆球星星icon.png",width:22.w ,height: 22.h,),
                            SizedBox(width: 5.w,),
                            Container(
                              width: screenSize.width-80.w,
                              child: Text("辛苦了，您的好评或建议，就是司机的动力～动动手指，帮助他们做的更好！",style: TextStyle(color: MyColors.themeColor,fontSize: 12.sp),),
                            ),
                          ],
                        ),
                        SizedBox(height: 16.h,),
                        Text("您的真实评价将帮助广大货主筛选优质司机",style: TextStyle(color: MyColors.authTipColor,fontSize: 12.sp),),
                        SizedBox(height: 22.h,),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Column(
                              children: [
                                Text("差评",style: TextStyle(color: MyColors.authTipColor,fontSize: 13.sp),),
                                SizedBox(height: 8.h,),
                                GestureDetector(
                                  child:chooseType==1?Image.asset("lib/Images/OrderList/差评彩色.png",width: 40.w,height: 40.h,): Image.asset("lib/Images/OrderList/差评灰色.png",width: 40.w,height: 40.h,),
                                  onTap: (){
                                    setState(() {
                                      chooseType=1;
                                      chooseList=[];
                                    });
                                  },
                                )
                              ],
                            ),
                            SizedBox(width:28.w ,),
                            Column(
                              children: [
                                Text("中评",style: TextStyle(color: MyColors.authTipColor,fontSize: 13.sp),),
                                SizedBox(height: 8.h,),
                                GestureDetector(
                                  child:chooseType==2?Image.asset("lib/Images/OrderList/中评彩色.png",width: 40.w,height: 40.h,): Image.asset("lib/Images/OrderList/中评灰色.png",width: 40.w,height: 40.h,),
                                  onTap: (){
                                    setState(() {
                                      chooseType=2;
                                      chooseList=[];
                                    });
                                  },
                                )
                              ],
                            ),
                            SizedBox(width:28.w ,),
                            Column(
                              children: [
                                Text("好评",style: TextStyle(color: MyColors.authTipColor,fontSize: 13.sp),),
                                SizedBox(height: 8.h,),
                                GestureDetector(
                                  child:chooseType==3?Image.asset("lib/Images/OrderList/好评彩色.png",width: 40.w,height: 40.h,): Image.asset("lib/Images/OrderList/好评灰色.png",width: 40.w,height: 40.h,),
                                  onTap: (){
                                    setState(() {
                                      chooseType=3;
                                      chooseList=[];
                                    });
                                  },
                                )
                              ],
                            ),
                          ],
                        ),
                        Container(
                          padding: EdgeInsets.only(left: 21.w,right: 22.w),
                          child:  chooseType==1?
                          Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Container(
                                  padding: EdgeInsets.only(left: 4.w,right: 4.w),
                                  height: 28.h,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(10)),
                                      image: DecorationImage(image: AssetImage('lib/Images/OrderList/评价气泡icon.png'),
                                          fit: BoxFit.cover)),
                                  child: Center(
                                    child: Text("不满意或非常不满意",style: (TextStyle(color: MyColors.themeColor,fontSize: 13.sp)),),
                                  )
                              ),
                            ],
                          ):chooseType==2?
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Container(
                                  padding: EdgeInsets.only(left: 4.w,right: 4.w),
                                  height: 28.h,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(10)),
                                      image: DecorationImage(image: AssetImage('lib/Images/OrderList/评价气泡icon.png'),
                                          fit: BoxFit.cover)),
                                  child: Center(
                                    child: Text("一般满意，希望下次可以做的更好",style: (TextStyle(color: MyColors.themeColor,fontSize: 13.sp)),),
                                  )
                              ),
                            ],
                          ):
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Container(
                                  padding: EdgeInsets.only(left: 4.w,right: 4.w),
                                  height: 28.h,
                                  decoration: BoxDecoration(
                                      borderRadius: BorderRadius.all(Radius.circular(10)),
                                      image: DecorationImage(image: AssetImage('lib/Images/OrderList/评价气泡icon.png'),
                                          fit: BoxFit.cover)),
                                  child: Center(
                                    child: Text("比较满意或非常满意",style: (TextStyle(color: MyColors.themeColor,fontSize: 13.sp)),),
                                  )
                              ),
                            ],
                          ) ,
                        ),
                        SizedBox(height: 27.h,),
                        Padding(
                          padding: const EdgeInsets.only( left: 18),
                          child: Wrap(
                            spacing: 8.0, // 两个Container之间的水平间距
                            runSpacing: 10.0, // 行与行之间的垂直间距
                            children: chooseType==1?
                           badMesList.map((length) {
                              bool? isSelected = chooseList.contains(length);

                              return GestureDetector(
                                onTap: () {
                                  // 处理点击事件
                                  setState(() {
                                    if (isSelected) {
                                      //如果已经被选中，则移除
                                      chooseList.remove(length);
                                    } else  {
                                      // 如果未被选中且未达到最大选择数量，则添加
                                      chooseList.add(length);
                                    }
                                  });
                                },
                                child: Container(
                                  width: 130.w, // Container宽度
                                  height: 30.h, // Container高度，使其成为正方形
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      color: isSelected ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                                      width: 1.0, // 边框宽度
                                    ),
                                    color: isSelected ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                                    borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                                  ),
                                  child: Center(
                                    child: Text(
                                      length,
                                      style: TextStyle(
                                        fontSize: 13.sp,
                                        color: isSelected ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                                      ),
                                    ),
                                  ),
                                ),
                              );
                            }).toList()
                                :chooseType==2?
                            midMesList.map((length) {
                              bool? isSelected = chooseList.contains(length);

                              return GestureDetector(
                                onTap: () {
                                  // 处理点击事件
                                  setState(() {
                                    if (isSelected) {
                                      //如果已经被选中，则移除
                                      chooseList.remove(length);
                                    } else  {
                                      // 如果未被选中且未达到最大选择数量，则添加
                                      chooseList.add(length);
                                    }
                                  });
                                },
                                child: Container(
                                  width: 130.w, // Container宽度
                                  height: 30.h, // Container高度，使其成为正方形
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      color: isSelected ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                                      width: 1.0, // 边框宽度
                                    ),
                                    color: isSelected ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                                    borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                                  ),
                                  child: Center(
                                    child: Text(
                                      length,
                                      style: TextStyle(
                                        fontSize: 13.sp,
                                        color: isSelected ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                                      ),
                                    ),
                                  ),
                                ),
                              );
                            }).toList()
                                : goodMesList.map((length) {
                              bool? isSelected = chooseList.contains(length);

                              return GestureDetector(
                                onTap: () {
                                  // 处理点击事件
                                  setState(() {
                                    if (isSelected) {
                                      //如果已经被选中，则移除
                                      chooseList.remove(length);
                                    } else  {
                                      // 如果未被选中且未达到最大选择数量，则添加
                                      chooseList.add(length);
                                    }
                                  });
                                },
                                child: Container(
                                  width: 130.w, // Container宽度
                                  height: 30.h, // Container高度，使其成为正方形
                                  decoration: BoxDecoration(
                                    border: Border.all(
                                      color: isSelected ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                                      width: 1.0, // 边框宽度
                                    ),
                                    color: isSelected ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                                    borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                                  ),
                                  child: Center(
                                    child: Text(
                                      length,
                                      style: TextStyle(
                                        fontSize: 13.sp,
                                        color: isSelected ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                                      ),
                                    ),
                                  ),
                                ),
                              );
                            }).toList(),
                          ),
                        ),
                        SizedBox(height: 8.h,),
                        Container(
                          padding: EdgeInsets.only(left: 22.w,right: 6.w),
                          child:BrnInputText(
                            maxHeight: 200.w,
                            minHeight: 35.h,
                            minLines: 1,
                            maxLength: 100,
                            bgColor: MyColors.backgreyColor,
                            textString:message,
                            textInputAction: TextInputAction.newline,
                            // maxHintLines: 20,
                            hint: '（非必填）您可以说出不满意的地方',
                            padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                            onTextChange: (text) {
                              setState(() {
                                message=text;
                              });
                            },
                            onSubmit: (text) {

                            },
                          ),
                        ),

                      ],
                    ),
                  ),
                ],
              ),
          ),
          Container(
            padding: EdgeInsets.only(left: 15.w,right: 15.w,top: 10.h,bottom: 40.h),
            height: 85.h,
            color: Colors.white,
            child: BrnSmallMainButton(
              bgColor: MyColors.themeColor,
              textColor: Colors.white,
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
              radius: 30,
              width: screenSize.width-30.w,
              title: '提交评价',
              onTap: () async{
                if(chooseType==-1){
                  BrnToast.show("请选择评价等级", context);
                }else {
                 ApiManager.postData(
                    context,
                    'api/auth/driver/evaluation/add',
                    {
                      "waybillId": widget.dataMap['id'],
                      "driverId": widget.dataMap['driverId'],
                      "shipperId": widget.dataMap['shipperId'],
                      "route": widget.dataMap['goodsInfo']['loadPlaceInfo'][0]['city']+widget.dataMap['goodsInfo']['loadPlaceInfo'][0]['county']+"-"+ widget.dataMap['goodsInfo']['unloadPlaceInfo'][0]['city']+widget.dataMap['goodsInfo']['unloadPlaceInfo'][0]['county'],
                      "level":chooseType==1?"NEGATIVE":chooseType==1?"NEUTRAL":"POSITIVE",
                      "tags": chooseList,
                      "description": message

                    },
                        (responseData) {
                      setState(() {
                        BrnToast.show("评价成功", context,
                          duration: BrnDuration.short,
                        );
                        Navigator.pushReplacement(
                          context,
                          MaterialPageRoute(
                            builder: (context) => TabbarView(pateIndex: 2,),
                            fullscreenDialog: true,),
                        );
                      });
                    },
                  );
                }
              },
            ),
          ),
        ],
      ),


    );

  }

}
